package cn.djb.day2;

import java.util.Stack;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) { val = x; }
 * }
 */
class Solution {

    /**
     * 用列表 list
     * @param head
     * @return
     */
/*    public int[] reversePrint(ListNode head) {
        if (null ==head)return new int[0];
        List<Integer> list = new ArrayList<>();
        list.add(0, head.val);
        ListNode next = head.next;
        while (null != next) {
            list.add(0, next.val);
            next = next.next;
        }
        if (list.size()>0){

            return list.stream().mapToInt(Integer::intValue).toArray();
        }
        return new int[0];
    }*/

    /**
     * 用栈
     *
     * @param head
     * @return
     */
    public int[] reversePrint(ListNode head) {

        Stack<Integer> stack = new Stack<>();

        ListNode next = head;
        while (null != next) {
            stack.push(next.val);
            next = next.next;
        }
        int size = stack.size();
        int[] print = new int[size];
        for (int i = 0; i < size; i++) {
            print[i] = stack.pop();
        }
        return print;

    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.reversePrint(null);
    }

}